/**
 * Created by edison on 2017/3/5.
 * 定义两个存取localStorage的方法
 */

const saveToLocal = (storageType, id, key, value) => {
  const store = storageType === 'localStorage' ? window.localStorage : window.sessionStorage
  let bizdesk = store.__bizdesk__
  if (!bizdesk) {
    bizdesk = {}
    bizdesk[id] = {}
    bizdesk[id][key] = value
  } else {
    bizdesk = JSON.parse(bizdesk)
    if (!bizdesk[id]) {
      bizdesk[id] = {}
    }
  }
  bizdesk[id][key] = value
  store.__bizdesk__ = JSON.stringify(bizdesk)
}
// def 为默认数据
const getFromLocal = (storageType, id, key, def) => {
  const store = storageType === 'localStorage' ? window.localStorage : window.sessionStorage
  let bizdesk = store.__bizdesk__
  if (!bizdesk) {
    return def
  }
  bizdesk = JSON.parse(bizdesk)[id]
  if (!bizdesk) {
    return def
  }
  if (bizdesk[key] === 'undefined') {
    return def
  }
  return bizdesk[key] || def
}
export {
  saveToLocal,
  getFromLocal
}
